import {
    fetchDeviceList, fetchRemoveBind 
} from '@/services/device';
import { showToast, showModal } from '@/utils/util';
import { customNavigate } from '@/utils/route'

const app = getApp();

Page({
    data: {
        pageHeight: 0,
        list: [],
        staticPrefix: app.globalData.STATIC_PREFIX
    },
    onLoad(options) {
        this.setData({
            pageHeight: wx.getWindowInfo().windowHeight - app.globalData.IPHONE_BAR_HEIGHT
        })
    },
    onShow() {
        this.searchList();
    },
    async searchList() {
        const res = await fetchDeviceList({
            id: wx.getStorageSync('elderlyInfo').id
        })
        let list = res.data.health_list.concat(res.data.home_list);
        this.setData({
            list
        })
    },
    onNavigate(event) {
        const { path, item } = event.currentTarget.dataset;
        let routeParams;
        switch (path) {
            case '/Device/pages/Info/index':
                routeParams = {
                    url: path,
                    params: {
                        device_imei: item.device_imei
                    }
                }
                break;
            case '/Device/pages/Bind/List/index':
                routeParams = {
                    url: path
                }
                break;
            default:
                break;
        }
        customNavigate({
            ...routeParams
        })
    },
    onUnpair(event) {
        const { item } = event.currentTarget.dataset;
        const params = {
            device_imei: item.device_imei
        }
        showModal('继续操作将进入解绑该设备流程，请确认是否继续操作？', async () => {
            const res = await fetchRemoveBind(params);
            if(res.return_code == 'SUCCESS') {
                showToast('设备已解绑', () => {
                    this.setData({
                        list: []
                    }, this.searchList)
                })
            }
        })
    }
})